package com.example.harmans;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class CalculatorActivity extends Activity {

    int i=1;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calculator);

		ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Interest Calculator");

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);
        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        // App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );

        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true); 

        // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

		drawerListView.setOnItemClickListener(new DrawerItemClickListener());

        TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
        tabs.setup();
        TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
        spec1.setContent(R.id.tab1layout);
        spec1.setIndicator("Including Interest");
        tabs.getTabWidget().setDividerDrawable(null);
        tabs.addTab(spec1);

        TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
        spec2.setContent(R.id.textView2);
        spec2.setIndicator("Excluding Interest");

        tabs.addTab(spec2);

        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = df.format(c.getTime());
        EditText todaysdate = (EditText)findViewById(R.id.todaysdate);
        todaysdate.setText(formattedDate);

        TextView p2Text = (TextView)findViewById(R.id.p2Text);
        LinearLayout payment2 = (LinearLayout)findViewById(R.id.payment2);
        TextView p3Text = (TextView)findViewById(R.id.p3Text);
        LinearLayout payment3 = (LinearLayout)findViewById(R.id.payment3);

        payment2.setVisibility(View.GONE);
        p2Text.setVisibility(View.GONE);
        payment3.setVisibility(View.GONE);
        p3Text.setVisibility(View.GONE);

        calculate();
	}

	public void calculate() {
		RelativeLayout addGlobal = (RelativeLayout)findViewById(R.id.addGlobal);
		addGlobal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TextView p2Text = (TextView)findViewById(R.id.p2Text);
		        LinearLayout payment2 = (LinearLayout)findViewById(R.id.payment2);
		        TextView p3Text = (TextView)findViewById(R.id.p3Text);
		        LinearLayout payment3 = (LinearLayout)findViewById(R.id.payment3);

		        if(payment2.isShown()) {
		        	payment3.setVisibility(View.VISIBLE);
		        	p3Text.setVisibility(View.VISIBLE);
		        } else {
		        	payment2.setVisibility(View.VISIBLE);
			        p2Text.setVisibility(View.VISIBLE);
		        }
			}
			
		});

		RelativeLayout removeGlobal = (RelativeLayout)findViewById(R.id.removeGlobal);
		removeGlobal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TextView p2Text = (TextView)findViewById(R.id.p2Text);
		        LinearLayout payment2 = (LinearLayout)findViewById(R.id.payment2);
		        TextView p3Text = (TextView)findViewById(R.id.p3Text);
		        LinearLayout payment3 = (LinearLayout)findViewById(R.id.payment3);

		        if(payment3.isShown()) {
		        	payment3.setVisibility(View.GONE);
		        	p3Text.setVisibility(View.GONE);
		        } else {
		        	payment2.setVisibility(View.GONE);
			        p2Text.setVisibility(View.GONE);
		        }
			}
			
		});

		TextView calculate = (TextView)findViewById(R.id.calculate);
		calculate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText todaysdate = (EditText)findViewById(R.id.todaysdate);
				Button orderdate = (Button)findViewById(R.id.orderdate);
				EditText offer = (EditText)findViewById(R.id.offer);
				EditText costs = (EditText)findViewById(R.id.costs);

				EditText date1 = (EditText)findViewById(R.id.p1Date);
				EditText amount1 = (EditText)findViewById(R.id.p1Amount);

				final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;

				String startdate = todaysdate.getText().toString();
				String enddate = orderdate.getText().toString();
				String costoffer = offer.getText().toString();
				Date dateone;
				Date datetwo;
				int diffInDays = 0;
				try {
					dateone = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).parse(startdate);
					System.out.println("date1 = "+dateone);
					datetwo = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH).parse(enddate);
					System.out.println("date2 = "+datetwo);
					diffInDays = (int) ((datetwo.getTime() - dateone.getTime())/ DAY_IN_MILLIS );
					System.out.println("diff = "+diffInDays);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				EditText totalvalue = (EditText)findViewById(R.id.totalvalue);
				int totalcost = Integer.parseInt(costoffer);
				diffInDays=(int) (totalcost+(((diffInDays/365)*0.08)*totalcost));
				totalvalue.setText(Integer.toString(diffInDays));
			}
		});
	}

	public void showDatePickerDialogPayment(View V) {
		DialogFragment newFragment = new DatePickerFragmentPayment();
	    newFragment.show(getFragmentManager(), "datePicker");
	}

	public void showDatePickerDialog(View v) {
	    DialogFragment newFragment = new DatePickerFragment();
	    newFragment.show(getFragmentManager(), "datePicker");
	}

	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

	private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            String link = (String) ((TextView)view).getText();
            System.out.println("VVV = "+link);

            if(link.equals("Home")) {
        		Intent homeActivity = new Intent(CalculatorActivity.this,MainActivity.class);
        		startActivity(homeActivity);
        		System.out.println("ZZZ = "+link);
            } else if(link.equals("Find A Court")) {
        		Intent courtActivity = new Intent(CalculatorActivity.this,CourtActivity.class);
		    	startActivity(courtActivity);
            } else if(link.equals("Court Fees")) {
        		Intent feesActivity = new Intent(CalculatorActivity.this,FeesActivity.class);
		    	startActivity(feesActivity);
            } else if(link.equals("Book A Collection")) {
        		Intent collectionActivity = new Intent(CalculatorActivity.this,CollectionActivity.class);
		    	startActivity(collectionActivity);
            } else if(link.equals("Interest Calculator")) {
        		Intent calcActivity = new Intent(CalculatorActivity.this,CalculatorActivity.class);
		    	startActivity(calcActivity);
            } else if(link.equals("FAQ")) {
        		Intent faqActivity = new Intent(CalculatorActivity.this,FaqActivity.class);
		    	startActivity(faqActivity);
            } else if(link.equals("Contact")) {
        		Intent contactActivity = new Intent(CalculatorActivity.this,ContactActivity.class);
		    	startActivity(contactActivity);
            } else if(link.equals("About")) {
    			Intent aboutActivity = new Intent(CalculatorActivity.this,AboutActivity.class);
		    	startActivity(aboutActivity);
            }
            drawerLayout.closeDrawer(drawerListView);
        }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.calculator, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			startActivity(new Intent(CalculatorActivity.this,MainActivity.class)); 
			return true;
		}
	    if(item.getItemId() == R.id.icon_calculator) {
	    	Intent calcIntent = new Intent(CalculatorActivity.this,CalculatorActivity.class);
	    	startActivity(calcIntent);
	    }
		if(item.getItemId() == R.id.menu_home) {	    	
			Intent mainIntent = new Intent(CalculatorActivity.this,MainActivity.class);
			startActivity(mainIntent);
	    }
	    if(item.getItemId() == R.id.menu_court) {
	    	Intent courtIntent = new Intent(CalculatorActivity.this,CourtActivity.class);
	    	startActivity(courtIntent);
	    }
	    if(item.getItemId() == R.id.menu_fees) {
	    	Intent feesIntent = new Intent(CalculatorActivity.this,FeesActivity.class);
	    	startActivity(feesIntent);
	    }
	    if(item.getItemId() == R.id.menu_collection) {
	    	Intent collectionIntent = new Intent(CalculatorActivity.this,CollectionActivity.class);
	    	startActivity(collectionIntent);
	    }
	    if(item.getItemId() == R.id.menu_faq) {
	    	Intent faqIntent = new Intent(CalculatorActivity.this,FaqActivity.class);
	    	startActivity(faqIntent);
	    }
	    if(item.getItemId() == R.id.menu_contact) {
	    	Intent contactIntent = new Intent(CalculatorActivity.this,ContactActivity.class);
	    	startActivity(contactIntent);
	    }
	    if(item.getItemId() == R.id.menu_about) {
	    	Intent aboutIntent = new Intent(CalculatorActivity.this,AboutActivity.class);
	    	startActivity(aboutIntent);
	    }
	    return true;
	}

	@SuppressLint("ValidFragment")
	public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = (c.get(Calendar.MONTH)+1);
			int day = c.get(Calendar.DAY_OF_MONTH);

			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getActivity(), this, year, month, day);
		}

		public void onDateSet(DatePicker view, int year, int month, int day) {
			// Do something with the date chosen by the user
			Button orderdate = (Button)findViewById(R.id.orderdate);
		    orderdate.setText(day+"-"+month+"-"+year);
		}
	}

	@SuppressLint("ValidFragment")
	public class DatePickerFragmentPayment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = (c.get(Calendar.MONTH)+1);
			int day = c.get(Calendar.DAY_OF_MONTH);

			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getActivity(), this, year, month, day);
		}

		public void onDateSet(DatePicker view, int year, int month, int day) {
			// Do something with the date chosen by the user
			Button orderdate = (Button)findViewById(R.id.orderdate);
		    orderdate.setText(day+"-"+month+"-"+year);
		}
	}
}
